﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace ProgNetComponents.Log
{
    public class Logger
    {
        private static Logger _Default = null;
        /// <summary>
        /// Used for default loggin string.Log()
        /// </summary>
        public static Logger Default
        {
            get
            {
                if (_Default == null)
                {
                    _Default = new Logger(System.Windows.Forms.Application.StartupPath + "\\" + System.Windows.Forms.Application.ProductName + ".log");
                    _Default.AutoClose = true;
                    _Default.AddDate = true;
                }
                return _Default;
            }
            set
            {
                _Default = value;
            }
        }

        private StreamWriter _Writer = null;
        private FileStream _File = null;
        public bool AddDate
        {
            get;
            set;
        }
        private  string _FileName;

        public string FileName
        {
            get { return _FileName; }
        }
        public string DateTimeFormat
        {
            get;
            set;
        }
        public bool AutoClose
        {
            get;set;
        }
        public Logger(string FileName)
        {
            _FileName = FileName;
            DateTimeFormat = System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat.ShortDatePattern + " " + System.Globalization.CultureInfo.CurrentUICulture.DateTimeFormat.ShortTimePattern;
        }
        public void OpenSession()
        {
            if (_Writer == null)
            {
                _File = new FileStream(_FileName, FileMode.Append);
                _Writer = new StreamWriter(_File);
            }
        }
        public void CloseSession()
        {
            if (_Writer != null)
            {
                _Writer.Close();
                _File.Close();
                _Writer.Dispose();
                _File.Dispose();
                _Writer = null;
                _File = null;
            }
        }

        public bool Log(string text)
        {
            bool result = true;
            try
            {
                OpenSession();
                if (AddDate)
                    text = "[" + DateTime.Now.ToString(DateTimeFormat) + "]: " + text;
                _Writer.WriteLine(text);
                _Writer.Flush();
            }
            catch { result = false; }
            if (AutoClose)
                CloseSession();
            return result;
        }
        public bool Log(Exception ex, string additionalMessage = null)
        {
            string msg = additionalMessage;
            if (ex != null)
            {
                msg = msg.AppendLine(ex.Message);
                msg = msg.AppendLine(ex.StackTrace);
            }
            return Log(msg);
        }
    }
}
